<template>
    <div style="margin: 20px;">
      <el-dialog :title="topic" :visible.sync="dialogFormVisible" style="float:left">
      
        <table> 
          <tr>
            <td>发票号</td>        
    
            <td><el-input v-model="newInvoiceId"  placeholder="发票号" :disabled='inputVisible'></el-input></td>
            <td><el-button v-if="modifyVisible" @click="inputVisible = false">修改发票号</el-button></td>
          </tr>
          
        </table>

        <el-table
          :data="selectedDataP"
          max-height="300"
          tooltip-effect="dark"
          style="width: 100%"
          :default-sort = "{prop: 'date'}"
          >
          <el-table-column
            prop="projectName"
            label="项目名"
            width="200">
          </el-table-column>
          <el-table-column
            prop="price"
            label="单价"
            width="120">
          </el-table-column>
          <el-table-column
            prop="projectNum"
            label="数量"
            width="120">
          </el-table-column>
          
          </el-table>
<hr/>
        <el-table
          :data="selectedDataD"
          max-height="300"
          tooltip-effect="dark"
          style="width: 100%"
          :default-sort = "{prop: 'date'}"
          >
          <el-table-column
            prop="drugName"
            label="药品名"
            width="200">
          </el-table-column>
          <el-table-column
            prop="drugUnitPrice"
            label="单价"
            width="120">
          </el-table-column>
          <el-table-column
            prop="drugNum"
            label="数量"
            width="120">
          </el-table-column>
          
          </el-table>
          
          <table>
            <tr>
            <td>
            总金额:</td>
            <td><el-input v-model="totalMoney"  placeholder="总金额" :disabled='true'></el-input>
            </td>
            </tr>
          </table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetVisible()">返 回</el-button>
        <div v-show="ifOperate">
        <el-button type="primary" v-if="printSelect" @click="registerFillInvoice()">打印</el-button>
        <el-button type="primary" v-else @click="registerReInvoice()">打印</el-button>
        </div>
      </div>
      
    </el-dialog>
        <el-container>           
        <el-header >        
          <span style="font-size:60px">发票补打/重打</span>
          <el-link type="primary" href="/#/initMain" style="float:right"><b>返回首页</b></el-link>
        </el-header>
        <el-main style="text-align:left">               
            <span ><b>发票信息查询</b></span><br/>
            <div>
            <el-form  :model="reInvoiceForm" :rules="rules" ref="reInvoiceForm" label-width="60px" class="demo-reInvoiceForm">  
                <el-row :gutter="20">
                    <el-col :span="8">            
                <el-form-item label="病历号" prop="patientId" >
                <el-input v-model="reInvoiceForm.patientId"  placeholder="病历号"></el-input>
                </el-form-item>
                    </el-col>
                  <!--  <el-col :span="8">            
                <el-form-item label="发票号" prop="invoiceId" >
                <el-input v-model="reInvoiceForm.invoiceId"  placeholder="发票号"></el-input>
                </el-form-item>
                    </el-col>-->
                    
                <el-button type="primary" @click="findPatientAndInvoice" icon="el-icon-search">查询</el-button>
                </el-row>
            </el-form>
            </div>

                <hr/>
                <span ><b>发票信息</b></span>
                
                 <el-table
                    :data="tableDataInvoice"
                    style="width: 100%"
                    max-height="500"
                    :default-sort = "{prop: 'date'}">
                    <el-table-column
                        prop="patientId"
                        label="病历号"
                        width="150">
                    </el-table-column>
                    <el-table-column
                        prop="name"
                        label="姓名"
                        width="120">
                    </el-table-column>
                    <el-table-column
                        prop="invoiceId"
                        label="发票号"
                        width="150">
                    </el-table-column>
                    <el-table-column
                    prop="invoiceDatetime"
                    label="发票日期"
                    sortable
                    width="150">
                    </el-table-column>
                    
                  <!--  <el-table-column
                    fixed="right"
                    label="操作"
                    width="120">
                    <template slot-scope="scope">
                        <el-button
                        @click.native.prevent="deleteRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                        退号
                        </el-button>
                    </template>
                    </el-table-column>-->
                    <el-table-column label="操作">
                      <template slot-scope="scope">
                        <el-button
                          size="mini"
                          @click="handleWatch( scope.row)">查看</el-button>
                        <el-button
                          size="mini"
                          type="danger"
                          @click="handleFill( scope.$index,scope.row)">补打</el-button>
                        <el-button
                          size="mini"
                          type="danger"
                          @click="handleRe( scope.row)">重打</el-button>
                      </template>
                    </el-table-column>
 
                </el-table>
                

              </el-main>
            </el-container>
   </div>
</template>

<style>
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
    
  }
</style>

<script>
import axios from "axios";
  export default {
    data() {
      return {
        
        topic:'',
        dialogFormVisible:false,
        inputVisible:true,
        modifyVisible:false,
        printSelect:false,
        ifOperate:false,
        totalMoney:0,
        oldInvoiceId:'',
        newInvoiceId:'',
        reInvoiceForm:{
            patientId:'' ,
            invoiceId:''      
        },
        rules: {
          patientId: [
            { required: true, message: '请输入病历号', trigger: 'blur' },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          },
        selectedDataP:[],
        selectedDataD:[],
        tableDataInvoice: [],
        invObject:'',
        location:0
      }
    },
    methods: {
      /*submitForm(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {
                alert('submit!');
            } else {
                console.log('error submit!!');
                return false;
            }
        });
      },*/
      findPatientAndInvoice(){
        axios
            .get("/api/reception/findRelatedInvoice", {
              params: {
                patientId: this.reInvoiceForm.patientId,
                //stateId:4
              }
            })
            .then(res => {
              console.log(res);
              this.tableDataInvoice=[];
              this.tableDataInvoice = res.data;
            })
            .catch(function(error) {
              console.log(error);
            });
          
      },
      handleWatch(row) {
        console.log(row);
        this.topic='查看发票';
        this.newInvoiceId=row.invoiceId;
        this.dialogFormVisible=true;
        this.modifyVisible=false;
        this.ifOperate=false;
        this.printSelect=false;
        this.findInvoiceDetail(row.invoiceId);
        this.totalMoney=row.money;
      },
      handleFill(index,row) {
        console.log(row);
        
        this.invObject=row;
        this.location=index;
        this.oldInvoiceId=row.invoiceId;
        this.newInvoiceId=row.invoiceId;
        this.topic='补打发票'
        this.dialogFormVisible=true;
        this.modifyVisible=true;
        this.ifOperate=true;
        this.printSelect=true;
        this.findInvoiceDetail(row.invoiceId);
        this.totalMoney=row.money;
        
        
    },
      handleRe(row) {
        console.log( row);
        this.invObject=row;
        console.log("invObject");
        console.log(this.invObject);
        this.newInvoiceId=row.invoiceId;
        this.topic='重打发票';
        this.dialogFormVisible=true;
        this.modifyVisible=false;
        this.ifOperate=true;
        this.printSelect=false;
        this.findInvoiceDetail(row.invoiceId);
        this.totalMoney=row.money;
    },
      findInvoiceDetail(invId){
        axios
        .get("/api/reception/findProDetail", {
          params: {
            invoiceId: invId,
          }
        })
        .then(res => {
          console.log(res.data);
          this.selectedDataP=[];
          this.selectedDataP=res.data;
        })
        .catch(function(error) {
                console.log(error);
        });
      axios
        .get("/api/reception/findDrugDetail", {
          params: {
            invoiceId: invId,
          }
        })
        .then(res => {
          console.log(res.data);
          this.selectedDataD=[];
          this.selectedDataD=res.data;
        })
        .catch(function(error) {
                console.log(error);
        });

      },
      //补打发票打印
    registerFillInvoice(){
      console.log(this.oldInvoiceId);
      console.log(this.newInvoiceId);
      if(this.oldInvoiceId!=this.newInvoiceId){
      var invoice=JSON.stringify({
        invoiceId:this.oldInvoiceId,
        //invoiceDatetime:new Date(),
        //payTypeId:this.payType,
        isLegal:0,
        //printTimes:1,
        //money:this.totalMoney,
        });
      console.log(invoice);
      axios
        .post("/api/reception/updateInvoice",invoice,{
          headers:{"Content-Type":"application/json;charset=utf-8"}
         
        })
        .then(res => {
          console.log(res.data);
          var invoice2=JSON.stringify({
        invoiceId:this.newInvoiceId,
        invoiceDatetime:new Date(),
        payTypeId:this.invObject.payTypeId,
        isLegal:1,
        printTimes:1,
        money:this.invObject.money,
        });
      console.log(invoice2);
      axios
      .post("/api/reception/registerInvoice",invoice2,{
        headers:{"Content-Type":"application/json;charset=utf-8"}
      }).then(res => {
      console.log(res.data);
      if(this.selectedDataP.length>0){
          this.selectedDataP.forEach(item=>{
            this.changePatiPro(item);
          });
      }
      if(this.selectedDataD.length>0){
          this.selectedDataD.forEach(item=>{
            this.changePatiDrug(item);
          });
      }
      //this.dialogFormVisible=false;
      
    }).catch(function(error) {
      console.log(error);
    });
    this.invObject.invoiceId=this.newInvoiceId;
    this.tableDataInvoice.splice(this.location,1,this.invObject);
  })
    .catch(function(error) {
      console.log(error);
    });
    }
    this.resetVisible();
      alert('打印成功');
  },
        
       //项目修改发票号
    changePatiPro(item){
      let patiPro=JSON.stringify({
          patientId:this.reInvoiceForm.patientId,
          projectName:item.projectName,
          invoiceId:this.newInvoiceId,
          //projectNum:this.item.projectNum,
          //projectDatetime:new Date() ,
          //stateId:8
        });
        console.log(patiPro);
        axios
        .post("/api/reception/updatePatiPro",patiPro,{
          headers:{"Content-Type":"application/json;charset=utf-8"}
          /*// params :{
          //   ppk:patiProKey,
          //   pp:patPro
          // }*/
        })
        .then(res => {
          console.log(res.data);
          /*this.tableDataM.splice(this.location,1);
          this.dialogFormVisible=false;*/
          })
        .catch(function(error) {
          console.log(error);
        });
    },
    //药品修改发票号
    changePatiDrug(item){
      let patiDrug=JSON.stringify({
          patientId:this.reInvoiceForm.patientId,
          drugId:item.drugId,
          invoiceId:this.newInvoiceId,
          //drugNum:item.drugNum,
          //drugDatetime:new Date(),
          //new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) 
          //stateId:8
        });
        console.log(patiDrug);
        axios
        .post("/api/reception/updatePatiDrug",patiDrug,{
          headers:{"Content-Type":"application/json;charset=utf-8"}
          /*// params :{
          //   ppk:patiProKey,
          //   pp:patPro
          // }*/
        })
        .then(res => {
          console.log(res.data);
          /*this.tableDataM.splice(this.location,1);
          this.dialogFormVisible=false;*/
          })
        .catch(function(error) {
          console.log(error);
        });
    },
    registerReInvoice(){
      var invoice=JSON.stringify({
        invoiceId:this.invObject.invoiceId,
        //invoiceDatetime:new Date(),
        //payTypeId:this.payType,
        //isLegal:0,
        printTimes:this.invObject.printTimes+1,
        //money:this.totalMoney,
        });
      console.log(invoice);
      axios
        .post("/api/reception/updateInvoice",invoice,{
          headers:{"Content-Type":"application/json;charset=utf-8"}
         
        })
        .then(res => {
          console.log(res.data);
          
          this.resetVisible();
          alert('打印成功');
      }).catch(function(error) {
          console.log(error);
        });
    },
    resetVisible(){
      this.dialogFormVisible=false;
      this.inputVisible=true;
      this.modifyVisible=false;
      this.printSelect=false;
    }

  }
    
    
} 
</script>